<template>
    <section>
      <div class="box-card" style="margin-top: 20px;">
        <!-- 查询区域 -->
        <div class="query-area">
          <div class="filter-item">
            <span class="label">类型</span>
            <el-select v-model="queryParams.type" placeholder="请选择" @change="handleTypeChange">
              <el-option v-for="item in typeOptions" :key="item.value" :label="item.name" :value="item.value"></el-option>
            </el-select>
          </div>
          <div class="action-buttons">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="success" @click="handleAdd">新增</el-button>
          </div>
        </div>
  
        <!-- 数据表格 -->
        <el-table :data="tableData" style="width: 100%" @cell-mouse-enter="cellMouseEnter"
              @cell-mouse-leave="cellMouseLeave" :header-cell-style="headerStyle" v-loading="loading">
          <el-table-column prop="id" label="序号" width="80"></el-table-column>
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column prop="value1" label="值1"></el-table-column>
          <el-table-column prop="value2" label="值2" ></el-table-column>
          <el-table-column prop="value3" label="值3" ></el-table-column>
          <el-table-column prop="dataSourceLabel" label="数据源" v-if="queryParams.type === 4"></el-table-column>
          <el-table-column label="类别">
            <template slot-scope="scope">
              {{ scope.row.typeName || getTypeNameByValue(scope.row.type) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="160">
            <template slot-scope="scope">
              <el-button size="small" type="primary" @click="handleEdit(scope.row)" :loading="loading">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(scope.row)" :loading="loading">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <!-- 添加/编辑表单弹窗 -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
          <!-- 类型1：月度销量 -->
          <el-form v-if="queryParams.type === 1" :model="formData" ref="dataForm" label-width="80px" :rules="rules">
            <el-form-item label="月份" prop="name">
              <el-input v-model="formData.name" placeholder="请输入月份"></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入数量"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型2：本月销量Top10 -->
          <el-form v-if="queryParams.type === 2" :model="formData" ref="dataForm" label-width="80px" :rules="rules">
            <el-form-item label="名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="金额" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入金额"></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="value2">
              <el-input v-model="formData.value2" placeholder="请输入数量"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型3：畅销大类占比 -->
          <el-form v-if="queryParams.type === 3" :model="formData" ref="dataForm" label-width="80px" :rules="rules">
            <el-form-item label="大类" prop="name">
              <el-input v-model="formData.name" placeholder="请输入大类"></el-input>
            </el-form-item>
            <el-form-item label="占比" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入占比"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型4：中间4项 -->
          <el-form v-if="queryParams.type === 4" :model="formData" ref="dataForm" label-width="80px" :rules="rules">
            <el-form-item label="数据源" prop="name">
              <el-select v-model="formData.name" placeholder="请选择数据源">
                <el-option v-for="item in dataSourceOptions" :key="item.value" :label="item.name" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="数据" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入数据"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型5：多级市场价格比对 -->
          <el-form v-if="queryParams.type === 5" :model="formData" ref="dataForm" label-width="80px" :rules="rules5">
            <el-form-item label="品名" prop="name">
              <el-input v-model="formData.name" placeholder="请输入品名"></el-input>
            </el-form-item>
            <el-form-item label="花卉刻" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入花卉刻"></el-input>
            </el-form-item>
            <el-form-item label="斗南" prop="value2">
              <el-input v-model="formData.value2" placeholder="请输入斗南"></el-input>
            </el-form-item>
            <el-form-item label="二级市场" prop="value3">
              <el-input v-model="formData.value3" placeholder="请输入二级市场"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型6：鲜花柜售出鲜花单数 -->
          <el-form v-if="queryParams.type === 6" :model="formData" ref="dataForm" label-width="80px" :rules="rules">
            <el-form-item label="名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入数量"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型7：售出价格 -->
          <el-form v-if="queryParams.type === 7" :model="formData" ref="dataForm" label-width="80px" :rules="rules">
            <el-form-item label="区间" prop="name">
              <el-input v-model="formData.name" placeholder="请输入区间"></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入数量"></el-input>
            </el-form-item>
          </el-form>
  
          <!-- 类型8：门店下单数量金额按月 -->
          <el-form v-if="queryParams.type === 8" :model="formData" ref="dataForm" label-width="80px" :rules="rules8">
            <el-form-item label="月份" prop="name">
              <el-input v-model="formData.name" placeholder="请输入月份"></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="value1">
              <el-input v-model="formData.value1" placeholder="请输入数量"></el-input>
            </el-form-item>
            <el-form-item label="金额" prop="value2">
              <el-input v-model="formData.value2" placeholder="请输入金额"></el-input>
            </el-form-item>
          </el-form>
  
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm">确定</el-button>
          </div>
        </el-dialog>
      </div>
    </section>
  </template>
  
  <script>
  import {
    dataBoardList,
    addDataBoard,
    updateDataBoard,
    deleteDataBoard,
      } from '@/api/api'
  export default {
    name: 'LargeScreenDataManagement',
    data() {
      return {
        queryParams: {
          type: 1
        },
        typeOptions: [
          { name: '1 月度销售', value: 1 },
          { name: '2 本月销量top10', value: 2 },
          { name: '3 畅销大类占比', value: 3 },
          { name: '4 中间4项', value: 4 },
          { name: '5 多级市场价格比对', value: 5 },
          { name: '6 鲜花柜售出鲜花单数', value: 6 },
          { name: '7 售出价格', value: 7 },
          { name: '8 门店下单数量金额按月', value: 8 }
        ],
        dataSourceOptions: [
          { name: '合伙人数量', value: 'data1' },
          { name: '总进行咨询量', value: 'data2' },
          { name: '合作花店数量', value: 'data3' },
          { name: '设备投放数量', value: 'data4' }
        ],
        tableData: [],
        loading: false,
        dialogVisible: false,
        dialogTitle: '新增数据',
        formData: {
          id: '',
          name: '',
          value1: '',
          value2: '',
          value3: '',
          type: '',
          dataSource: ''
        },
        isEdit: false,
        // 表单验证规则
        rules: {
          name: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value1: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value2: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ]
        },
        // 类型5的验证规则
        rules5: {
          name: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value1: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value2: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value3: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ]
        },
        // 类型8的验证规则
        rules8: {
          name: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value1: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ],
          value2: [
            { required: true, message: '此项为必填项', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      showValue2Column() {
        return [2, 5, 8].includes(this.queryParams.type)
      },
      showValue3Column() {
        return [5].includes(this.queryParams.type)
      }
    },
    methods: {
      // 处理类型变更
      handleTypeChange() {
        this.loadTableData()
      },
      
      // 加载表格数据
      loadTableData() {
        this.loading = true
        // 调用列表接口
        dataBoardList({ type: this.queryParams.type })
          .then(res => {
            if (res.data && res.data.data) {
              // 处理返回的数据，确保类型显示正确
              this.tableData = res.data.data.map(item => {
                // 如果返回的数据没有typeName，则根据type设置
                if (!item.typeName && item.type) {
                  item.typeName = this.getTypeNameByValue(item.type)
                }
                return item
              })
            } else {
              this.tableData = []
            }
          })
          .catch(() => {
            this.$message.error('获取数据失败')
            this.tableData = []
          })
          .finally(() => {
            this.loading = false
          })
      },
      
      // 查询按钮
      handleSearch() {
        this.loadTableData()
      },
      
      // 新增按钮
      handleAdd() {
        this.isEdit = false
        this.dialogTitle = '新增数据'
        this.initFormData()
        this.dialogVisible = true
      },
      
      // 初始化表单数据
      initFormData() {
        // 基础表单数据
        this.formData = {
          id: '',
          name: '',
          value1: '',
          value2: '',
          value3: '',
          type: this.getTypeLabel()
        }
        
        // 类型4需要默认选择第一个数据源
        if (this.queryParams.type === 4 && this.dataSourceOptions.length > 0) {
          this.formData.name = this.dataSourceOptions[0].value
        }
      },
      
      // 编辑按钮
      handleEdit(row) {
        this.isEdit = true
        this.dialogTitle = '编辑数据'
        
        // 复制一份数据，避免直接修改表格数据
        this.formData = JSON.parse(JSON.stringify(row))
        
        // 打开编辑弹窗
        this.dialogVisible = true
      },
      
      // 删除按钮
      handleDelete(row) {
        this.$confirm('确认删除该数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          deleteDataBoard({ id: row.id })
            .then(res => {
              if (res.data && res.data.httpCode == 200) {
                this.$message.success('删除成功')
                this.loadTableData() // 重新加载数据
              } else {
                this.$message.error(res.data.msg || '删除失败')
              }
            })
            .catch(() => {
              this.$message.error('删除失败')
            })
            .finally(() => {
              this.loading = false
            })
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      },
      
      // 表单提交
      submitForm() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.loading = true
            
            // 处理表单数据
            const formData = { ...this.formData }
            
            // 根据不同类型处理数据
            switch(this.queryParams.type) {
              case 4:
                // 中间4项类型，需要处理数据源
                formData.dataSourceLabel = this.getDataSourceLabel(formData.name)
                break
            }
            
            // 设置类型值（数字）和类型名称
            formData.type = this.queryParams.type
            formData.typeName = this.getTypeNameByValue(this.queryParams.type)
            
            if (this.isEdit) {
              // 编辑现有数据
              updateDataBoard(formData)
                .then(res => {
                  if (res.data && res.data.httpCode == 200) {
                    this.$message.success('更新成功')
                    this.dialogVisible = false
                    this.loadTableData() // 重新加载数据
                  } else {
                    this.$message.error(res.data.msg || '更新失败')
                  }
                })
                .catch(() => {
                  this.$message.error('更新失败')
                })
                .finally(() => {
                  this.loading = false
                })
            } else {
              // 添加新数据
              addDataBoard(formData)
                .then(res => {
                  if (res.data && res.data.httpCode == 200) {
                    this.$message.success('添加成功')
                    this.dialogVisible = false
                    this.loadTableData() // 重新加载数据
                  } else {
                    this.$message.error(res.data.msg || '添加失败')
                  }
                })
                .catch(() => {
                  this.$message.error('添加失败')
                })
                .finally(() => {
                  this.loading = false
                })
            }
          } else {
            return false
          }
        })
      },
      
      // 获取数据源标签
      getDataSourceLabel(value) {
        const found = this.dataSourceOptions.find(item => item.value === value)
        return found ? found.name : ''
      },
      
      // 根据选择的类型获取类型标签
      getTypeLabel() {
        const found = this.typeOptions.find(item => item.value === this.queryParams.type)
        return found ? found.name.substring(2) : ''
      },
      
      // 根据类型值获取类型名称
      getTypeNameByValue(typeValue) {
        if (!typeValue) return ''
        
        // 尝试将类型值转为数字
        const typeNum = Number(typeValue)
        if (isNaN(typeNum)) return typeValue
        
        const found = this.typeOptions.find(item => item.value === typeNum)
        return found ? found.name : typeValue
      }
    },
    created() {
      // 初始化时加载数据
      this.loadTableData()
    }
  }
  </script>
  
  <style scoped>
  .query-area {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }
  
  .filter-item {
    display: flex;
    align-items: center;
  }
  
  .filter-item .label {
    margin-right: 10px;
  }
  
  .action-buttons .el-button {
    margin-left: 10px;
  }
  </style> 